package com.example.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(value = {Mode.AverageTime,Mode.SingleShotTime})
@State(Scope.Thread)
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 5)
public class JmhHello {

     int count = 0;

     int count2 = 0;

    String string = "";
    StringBuilder stringBuilder = new StringBuilder();

    @Benchmark
    public String stringAdd(){
       count++;
        for(int i = 0;i<1000;i++){
            string = string+i;
        }
        System.out.println("=========stringAdd:"+count);
        return string;
    }

    @Benchmark
    public String stringBuilderAppend(){
        count2++;
        for(int i = 0;i<1000;i++){
            stringBuilder.append(i);
        }
        System.out.println("=========stringBuilderAppend:"+count2);
        return stringBuilder.toString();
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(JmhHello.class.getSimpleName())
                .build();
        new Runner(options).run();
        System.out.println("==================count:"+new JmhHello().count);
        System.out.println("==================count2:"+new JmhHello().count2);
    }
}
